import os,sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, "apps"))
sys.path.insert(0, os.path.join(BASE_DIR, "apps_extra"))

SECRET_KEY = 'd(e5061*j=w8=o3a9-z$on+xnxz45!axte$1*ftvp3%7u@3r-a'
DOMAIN_NAME = "http://127.0.0.1:8000"

ROOT_URLCONF = 'mx.urls'
WSGI_APPLICATION = 'mx.wsgi.application'


# 重写系统authenticate方法,实现用户名、邮箱、手机号登录
AUTHENTICATION_BACKENDS = ("apps.users.views.CustomBackend",)
# 使用自定义用户模型覆盖系统默认User模型进行身份验证
AUTH_USER_MODEL = "users.UserProfile"

# Application definition
INSTALLED_APPS = [
    # 'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #第三方应用
    'xadmin',
    'DjangoUeditor',
    'crispy_forms',
    'captcha',
    'pure_pagination',
    #注册自定义应用
    'apps.users',
    'apps.courses',
    'apps.organizations',
    'apps.operations',
]

MIDDLEWARE = [
    #'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    #'django.middleware.cache.FetchFromCacheMiddleware',
]


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                # 在模板对象中注册MEDIA_URL全局变量
                'django.template.context_processors.media',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mx',
        'USER': 'lixing',
        'PASSWORD': 'li1234mysql',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False

# 分页插件
PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,#分页列表中间显示10个分页
    'MARGIN_PAGES_DISPLAYED': 2,#分页列表两头分别显示2个分页
    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}

# 发送邮箱配置
EMAIL_HOST = "smtp.sina.com"
EMAIL_PORT = 25
EMAIL_HOST_USER = "lixing_java@sina.com"
EMAIL_HOST_PASSWORD = "li1234xl"
EMAIL_USE_TLS = False
EMAIL_FROM = "lixing_java@sina.com"

#静态文件
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) #DEBUG = True
# STATIC_ROOT = os.path.join(BASE_DIR, 'static') #DEBUG = False
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

"""
日志
def get(self,request):
    try:
        return render(request, "userLogin.html")
    except Exception as e:
        logging.getLogger("custom").info("users.views.LoginView:get  {0}".format(e))
        raise Http404 #DEBUG=True跳转到系统404页面,DEBUG=False跳转到templates下的404页面
"""
DEBUG = True
# DEBUG = False
ALLOWED_HOSTS = ['*']
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'simple': {'format': '%(asctime)s   %(levelname)s   %(message)s'},
    },
    'filters': {
        'debug_true': {'()': 'django.utils.log.RequireDebugTrue',},
        'debug_false': {'()': 'django.utils.log.RequireDebugFalse',},
    },
    #日志输出到控制台、文件、邮件发送
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'formatter': 'simple',
            'filters': ['debug_true'],
            'class': 'logging.StreamHandler',
        },
        'file': {
             'level': 'INFO',
             'formatter': 'simple',
             # 'filters': ['debug_false'],
             'class': 'logging.handlers.RotatingFileHandler',
             'filename': os.path.join(BASE_DIR, 'logs', 'mx.log'),
             'maxBytes': 1024*1024*1, # 1M
             'backupCount': 5,
        }
    },
    #日志类型
    'loggers': {
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'custom': {
            'level': 'INFO',
            'handlers': ['file']
        }
    }
}